<%@page import="beans.Account"%>
<%@page import="beans.ClassBean"%>
<%@page import="dao.MngClass"%>
<%@page import="dao.CheckAccount"%>
<%@page import="beans.Assignment"%>
<%@page import="dao.MngAssignment"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="java.util.List"%>

<html>
    <head>
        <script language="javascript">
            function delete1()
            {
                return confirm('Do you want delete this record');
            }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Show Assignment Page</title>
        <style>                        
            table, th, td {
                border: 1px solid #619702;
                border-collapse: collapse;
                width:99.3% ;
            }
            th, td {
                padding: 5px;
                width: 100px;
            }
            th {
                text-align: center;
            }
        </style>
    </head>
    <body> 
        <h2>List Assignment</h2>
        <%
            MngClass mngClass = new MngClass();
            CheckAccount ck = new CheckAccount();
            MngAssignment mngAssign = new MngAssignment();
            HttpSession session_ass = request.getSession();
            String acc_id_assign = session_ass.getAttribute("acc_id").toString();
            String roll_assign = session_ass.getAttribute("roll").toString();
            List<Assignment> listAssign = null;
            if (roll_assign.equals("student")) {
                listAssign = mngAssign.showAssignByAccount(acc_id_assign);
            } else {
                if (request.getAttribute("class_id") == null) {
                    listAssign = mngAssign.showAssign();
                } else { // roll = staff
                    if (request.getAttribute("assign_name") == null || request.getAttribute("assign_name").equals("")) {
                        listAssign = mngAssign.showAssignByClass(request.getAttribute("class_id").toString());
                    } else {
                        listAssign = mngAssign.showAssignByClassAndName(request.getAttribute("class_id").toString(), request.getAttribute("assign_name").toString());
                    }
                    if (roll_assign.equals("staff")) {
        %>                  
        <form action="showAssignByName" method="get">
            <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id").toString()%>">
            Please choose Assignment to delete,update and upload assign !<br>
            <select name="assign_name">
                <%
                    List<String> listAssignname = mngAssign.listAssignNameByClass(request.getAttribute("class_id").toString());
                %>
                <option value="">All Assignment</option>  
                <%
                    for (String assign_name : listAssignname) {
                %>
                <option value="<%= assign_name%>"><%= assign_name%></option> 
                <%
                    }
                %>
            </select>
            <input type="submit" value="Show" >
        </form>
        <%
                    }
                }
            }
            if (request.getAttribute("msg") != null) {
        %>
        <font color="green" > <%= request.getAttribute("msg")%> </font><br><br>
        <% }
            if (request.getAttribute("error") != null) {
        %>
        <font color="red" > <%= request.getAttribute("error")%> </font>
        <%
            }
            if (roll_assign.equals("staff")) {
                List<Account> listAcc = ck.listAccByClassId(request.getAttribute("class_id").toString());
                if (listAcc != null && (listAcc.size() > 0)) {

        %>
        <form action="toCreateAssign" method ="get">
            <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id").toString()%>">           
            <input type="submit" value="Create new assignment">
        </form><br>
        <%  } else {
        %>
        <font color="red" >Please add student in class to create Assignment !</font>
        <%
            }
        } else {%>        
        <br>
        <form action="editAssign" method="get">
            <input type="hidden" name="action" value="show_by_class">
            <select name="class_id">
                <option value="">Select Class</option>
                <%
                    List<ClassBean> listClass = mngClass.showClass();
                    for (ClassBean classBean : listClass) {
                %>
                <option value="<%= classBean.getClass_id()%>"><%= classBean.getClass_name()%></option>
                <% }%>
            </select>
            <input type="submit" value="Show" >
        </form>       
        <br>     
        <% }%>
        <%
            if ((request.getAttribute("assign_name") != null) && (!request.getAttribute("assign_name").equals("")) && roll_assign.equals("staff")) {%>  
        <form action="editAssign" method ="get">
            <input type="hidden" name="action" value="update">           
            <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id").toString()%>"> 
            <input type="hidden" name="assign_name" value="<%= request.getAttribute("assign_name").toString()%>">
            <input type="submit" value="Update">
        </form>    <br>    
        <form action="editAssign" method ="get">
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id").toString()%>">
            <input type="hidden" name="assign_name" value="<%= request.getAttribute("assign_name").toString()%>">
            <input type="submit" value="Delete" onclick='return delete1()'>
        </form>
        <br> 
        <form action="uploadAssign" method="post" enctype="multipart/form-data">            
            <input type="hidden" name="class_id" value="<%= request.getAttribute("class_id").toString()%>">
            <input type="hidden" name="assign_name" value="<%= request.getAttribute("assign_name").toString()%>">
            <input type="file" name="file" />
            <input type="submit" value="Upload" />
        </form>
        <br>
        <% }%>

        <table>            
            <tr>
                <th>Student</th>               
                <th>Assignment Name</th>                
                <th>Create Date</th>
                <th>Finish Date</th>                
                <th>Assignment</th>     
                <th>Finish Assignment</th>
                    <%  if (roll_assign.equals("staff")) { %>
                <th>Set Mark</th>
                    <% } else {
                    %>
                <th>Mark</th>    
                    <% }  %>
            </tr> 
            <%  for (Assignment assign : listAssign) {
                    if ((roll_assign.equals("staff")
                            && mngClass.getAccIdByClassId(assign.getClass_id()).equals(acc_id_assign))
                            || roll_assign.equals("student") || roll_assign.equals("admin")) {
                        if (assign.getMark() == null) {
                            assign.setMark("");
                        }
            %>
            <tr>
                <th><%= ck.getFullName(assign.getAcc_id())%></th>                
                <th><%= assign.getAssign_name()%></th>                
                <th><%= assign.getCreate_date()%></th>
                <th><%= assign.getFinish_date()%></th>                              
                <th>
                    <% if (roll_assign.equals("staff")) { //upload assignment
                            if ((assign.getFile_ass() != null) && (!assign.getFile_ass().equals(""))) {
                    %>        
                    File Assignment : <%= assign.getFile_ass()%>
                    <% }
                    } else if ((assign.getFile_ass() != null) && (!assign.getFile_ass().equals(""))) { //download assignment
                        String link_download_ass = "downloadAssign?file_name=" + assign.getFile_ass();
                    %>
                    <a href="<%= link_download_ass%>">Download</a>
                    <%
                        }
                    %>
                </th>
                <th>
                    <% if (roll_assign.equals("student")) { //upload finish assignment
                            if ((assign.getFile_finishass() != null) && !assign.getFile_finishass().equals("")) {
                    %>         
                    File Finish Assignment : <%= assign.getFile_finishass()%>        
                    <% }%>
            <form action="uploadFinishAssign" method="post" enctype="multipart/form-data">
                <input type="hidden" name="assign_id" value="<%= assign.getAssign_id()%>">             
                <input type="file" name="file" />
                <input type="submit" value="Upload" />
            </form>
            <%
            } else if ((assign.getFile_finishass() != null) && !assign.getFile_finishass().equals("")) { //download finish assignment
                String link_download_finishass = "downloadFinishAssign?file_name=" + assign.getFile_finishass();
            %>
            <a href="<%= link_download_finishass%>">Download</a>
            <% } %>
        </th>    
        <%  if (roll_assign.equals("staff")) {
        %>
        <th>             
        <form action="setMarkAssign" method="get">
            <input type="hidden" name="class_id" value="<%= assign.getClass_id()%>"> 
            <input type="hidden" name="assign_id" value="<%= assign.getAssign_id()%>">                                     
            <input type="text" size="2"  name="mark" value="<%= assign.getMark()%>"> 
            <input type="submit" value="Set Mark">
        </form>
    </th>
    <% } else {%>
    <th>
        <%= assign.getMark()%>
    </th>
    <% } %>   
</tr>   
<% }
    }%>
</table>
</body>
</html>
